<?php

namespace Data;

class Advertising extends Base {

    /**
     * 首页左边
     */
    const HOMELEFT = 1;

    /**
     * 首页右边
     */
    const HOMERIGHT = 2;

    /**
     * 添加广告
     *
     * @param $data
     *
     * @return mixed返回Id
     */
    public function addAdvertising($data) {
        $id = $this->instance()->Insert($data);
        return $id;
    }

    /**
     * 获取全部广告列表
     *
     * @param $pager 分页参数
     *
     * @return array 返回广告数组
     */
    public function getAllAdvertising($pager) {
        return $this->pager($pager)->findAll();
    }

    /**
     * 根据广告类型（位置）获取广告
     * @param $type
     * @param $count
     * @return array
     */
    public function getAdvertisingByType($type, $count = 1) {
        $where = array('Status' => self::IS_YES, 'Type ' => $type);
        return $this->where($where)->order('CreateTime DESC')->limit($count)
            ->findAll();
    }
    /**
     * 更具广告名称获取广告信息
     *
     * @param $name 广告名称
     *
     * @return array 返回广告数组
     */
    public function getAdvertisingByName($name) {
        $where = array('Name' => $name);
        $info = $this->select('Id, Name, Status')->where($where)->findAll();
        return $info;
    }

    /**
     * 根据广告标题获取广告信息
     *
     * @param $title 广告标题
     *
     * @return array 返回广告数组
     */
    public function getAdvertisingByTitle($title) {
        $where = array('Title' => $title);
        $info = $this->select('Id, Name, Status')->where($where)->findAll();
        return $info;
    }

    public function getAboutContentByTitle($title) {
        $where = array('Title' => $title);
        $info = $this->select('Content')->where($where)->findOne();
        return $info;
    }

    /**
     * 根据Id获取广告详情
     *
     * @param $id 广告Id
     *
     * @return array 返回广告详情数组
     */
    public function getAdvertisingById($id) {
        $where = array('Id' => $id);
        $info = $this->where($where)->findOne();
        return $info;
    }

    /**
     * 根据Id修改广告信息
     *
     * @param $id 广告Id
     *
     * @param $data 广告信息
     */
    public function updateAdvertising($id, $data) {
        $where = array('Id' => $id);
        $this->where($where)->update($data);
    }

    /**
     * 根据Id删除广告
     *
     * @param $id
     */
    public function delAdvertisingById($id) {
        $where = array('Id' => $id);
        $this->where($where)->del();
    }

    /**
     * 根据分类Id获取广告
     *
     * @param $categoryId
     * @return array
     */
    public function getAllAdvertisingByCategoryId($categoryId) {
        if(is_array($categoryId)) {
            $where = array('CategoryId in ' => $categoryId);
        } else {
            $where = array('CategoryId ' => $categoryId);
        }
        return $this->where($where)->findAll();
    }

    public function getAdvertisingIdByCategoryId($categoryId) {
        $where = array('CategoryId ' => $categoryId);
        $res =  $this->select('Id')->where($where)->order('Id DESC')->findOne();
        return $res ? $res['Id'] : 0;
    }

    /**
     * 获取广告位置
     * @return array
     */
    public function getAdvertisingPosition() {
        return array(
            self::HOMELEFT => '首页左边',
            self::HOMERIGHT => '首页右边',
            '3' => '左方',
            '4' => '右方');
    }
}